package com.syni.mdd.yhd.common.all.listen.groupShare;

import com.syni.mdd.yhd.common.all.config.AuroraConfig;
import com.syni.mdd.yhd.common.all.constants.AuroraPushCode;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessCoupon;
import com.syni.mdd.yhd.common.all.entity.BmsDxNews;
import com.syni.mdd.yhd.common.all.entity.groupChat.BmsDxGroupChat;
import com.syni.mdd.yhd.common.all.entity.groupChat.BmsUserChat;
import com.syni.mdd.yhd.common.all.listen.groupMessage.GroupMessageEvent;
import com.syni.mdd.yhd.common.all.service.BmsBusinessGroupMessageService;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.yhd.common.all.service.BmsDxNewsService;
import com.syni.mdd.yhd.common.all.service.groupChat.BmsUserChatService;
import com.syni.mdd.yhd.common.all.utils.aurora.AuroraPushUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
public class GroupShareListener implements ApplicationListener<GroupShareEvent> {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsUserChatService bmsUserChatService;

    @Autowired
    BmsDxNewsService bmsDxNewsService;

    @Override
    @Async
    public void onApplicationEvent(GroupShareEvent groupShareEvent){
        BmsDxGroupChat bmsDxGroupChat = groupShareEvent.getBmsDxGroupChat();
        BmsBusinessCoupon businessCoupon = groupShareEvent.getBmsBusinessCoupon();
        List<BmsUserChat> bmsUserChats = bmsUserChatService.getBaseDao().findByChatIdAndRoleId(bmsDxGroupChat.getId(),Constants.GENERAL_USER_ROLE_ID);
        List<String> audAlias = new LinkedList<>();
        if(bmsUserChats != null && bmsUserChats.size() > 0){
            for(BmsUserChat bmsUserChat : bmsUserChats){
                if(bmsUserChat.getIsStartRemind() == Constants.COMMON_FLAG_TRUE_INT) {
                    audAlias.add(AuroraConfig.USER_PREFIX + bmsUserChat.getUserId());
                }
            }
        }
        if(audAlias.size() > 0) {
            AuroraPushUtils.sendMessageToAppByAlias("all", audAlias.toArray(new String[audAlias.size()]),
                    AuroraPushCode.USER_RECE_CHAT_COUPON, businessCoupon, "有新的优惠券消息", new HashMap<>(), Constants.GENERAL_USER_ROLE_ID);
        }
    }
}
